$(document).ready(function(){
    $('#top_keyword').focus(function(){
        $(this).css('color', '#333');
        if ($(this).val() == $(this).context.defaultValue){
            $(this).val("");
        }
    }).blur(function(){
        if ($(this).val() == ""){
            $(this).val($(this).context.defaultValue);
            $(this).css('color', '#838383');
        }
    }); 
    $("#top_keyword").keyup(function() 
    {
        var searchbox = $(this).val();
        var dataString = 'q='+searchbox;
        if(searchbox!='')
        {
            $("#topsrch_loading").css("display","block");
            $("#srch_display .view_more").hide();
            $.ajax({
                type: "GET",
                url: "/thanh-vien?action=getnames",
                data: dataString,
                cache: true ,               
                success: function(html)
                {
                    $("#srch_li").html(html);
                    $("#srch_display").show();
                }

            });
            $("#topsrch_loading").hide();
            $("#srch_display .view_more").css("display","block");
        }
    });
    $(document).click(function() {
        $("#srch_display").hide();
    });

    $(".mn_login").click(function(){ 
        $("#loginpopup").toggle();
        $(this).toggleClass("borderBlue");
    });
    $(".mn_account").click(function(){
        $("#annpopup").hide();     
        $("#msgpopup").hide();
        $(".mn_message").removeClass("borderBlue");
        $(".mn_announce").removeClass("borderBlue");
        $("#accpopup").toggle(); 
        $(this).toggleClass("borderBlue");
    });
    $(".mn_message").click(function(){
        $("#msg_loading").show();
        $("#accpopup").hide();
        $("#annpopup").hide();
        $(".mn_account").removeClass("borderBlue");
        $(".mn_announce").removeClass("borderBlue");
        $("#msgpopup").toggle(); 
        var data = "";
        data = data+'&type=message';
        $.ajax({
            type: "POST",
            dataType: 'json',
            url: "/process.php",
            data: "action=load_header_mail"+data,
            success: function(msg){
                if(msg.status=='ok'){
                    $('#msglist').html(msg.html);
                }else{
                    $('#msglist').html("Hiện tại bạn không có tin nhắn nào.");
                }
            }
        });
        $(this).toggleClass("borderBlue");
        $(this).parent().find(".notif-ctn").hide();
        $("#msg_loading").hide();
    });
    $(".mn_announce").click(function(){
        $("#not_loading").show();
        $("#accpopup").hide();
        $("#msgpopup").hide();
        $(".mn_message").removeClass("borderBlue");
        $(".mn_account").removeClass("borderBlue");
        $("#annpopup").toggle(); 
        var data = "";
        data = data+'&type=notification';
        $.ajax({
            type: "POST",
            dataType: 'json',
            url: "/process.php",
            data: "action=load_header_mail"+data,
            success: function(msg){
                if(msg.status=='ok'){
                    $('#notlist').html(msg.html);
                }else{
                    $('#notlist').html("Hiện tại bạn không có thông báo nào.");
                }
            }
        });
        $(this).toggleClass("borderBlue");
        $(this).parent().find(".notif-ctn").hide();
        $("#not_loading").hide();
    });
    $('.cq_activity').hover(function(){
        $(this).find(".dragger_container").show();   
    },function(){
        $(this).find(".dragger_container").hide(200);      
    });
    var cnt = 0;
    $('.pic_container #pic_slide li').each(function(){
        ++cnt;
        //window width
        var w_width 	= $(window).width();
        var spaces 	= w_width/(4+1);
        var left	= (spaces*cnt) - (140/2);
        var r 		= Math.floor(Math.random()*41)-20;
        var content = $(this);
        content.stop().animate({
            'left':'0px'
        },500,function(){            
            $(this).unbind('mouseenter')
            .bind('mouseenter',increaseSizeImage)
            .unbind('mouseleave')
            .bind('mouseleave',decreaseSizeImage);
        }).find('img')
        .stop()
        .animate({
            'rotate': r+'deg'
        },300);
    })
   
});   

function showSendMsgTo(to_id){
 
    Lightbox.show(domain+"/thanh-vien?mid=4&action=guithu&to="+to_id,480,215,"Viết tin nhắn","iframe",true);

}
function showSendMsg(){
 
    Lightbox.show(domain+"/thanh-vien?mid=4&action=guithu",480,315,"Viết tin nhắn","iframe",true);

}
function showUploadImage(){
    Lightbox.show(domain+"/thanh-vien?mid=4&action=upload_img",740,370,"Tải ảnh và chia sẻ","iframe",false);
}
function showUploadImage_ID(album_id){
    Lightbox.show(domain+"/thanh-vien?mid=4&action=upload_img&aid="+album_id,740,370,"Tải ảnh và chia sẻ","iframe",false);
}
function showAddFriend(to_id){
    Lightbox.show(domain+"/thanh-vien?mid=4&action=addfriend&to="+to_id,400,215,"Gửi lời mời kết bạn","iframe",true);

}
function showMessageError(message){
    $('#message_alert').html(message);
    $('#message_alert').removeClass('success').addClass('error');
    $('#message_alert').show();
}
function showMessageSuccess(message){
    $('#message_alert').html(message);
    $('#message_alert').removeClass('error').addClass('success');
    $('#message_alert').show();
}
/**slide toggle menu**/
function slide_toogle_menu(obj)
{        
    var $parent = $(obj).parents('li.have_sub_menu');    
    if ( $parent.find('ul.submenu').css('display') == 'none' ) {
        $parent.children('ul.submenu').slideDown(200);
        $parent.children('a').addClass('over');
        $('ul.submenu a').removeClass('over');
    } else {
        $parent.children('ul.submenu').slideUp(200);
        $parent.children('a').removeClass('over');
    }
}
function likeThis(stream_id)
{
    var data = "";
    data = data+'&stream_id='+stream_id;
    $.ajax({
        type: "POST",
        dataType: 'json',
        url: "/process.php",
        data: "action=like_this"+data,
        success: function(msg){
            if(msg.status=='ok'){
                if($("#like_"+stream_id).hasClass("like"))
                {   
                    $("#like_"+stream_id).removeClass("like");
                    $("#like_"+stream_id).addClass("liked");
                }
                else
                {  
                    $("#like_"+stream_id).removeClass("liked");
                    $("#like_"+stream_id).addClass("like");
                }
            }else{
                alert('Bạn phải đăng nhập trước khi bình luận!');
            }
        }
    });
}
jQuery(function($){
    $("#top_keyword").Watermark("Search");
});
//mouseenter event on each thumb
function upImage(){
    var $content 	= $(this).find('a');
    $content.stop().animate({
        'marginTop'		: '-20px'
    },400).find('img')
    .stop()
    .animate({
        'rotate': '0deg'
    },400);
}
function increaseSizeImage(){
      var r      
    var $content    =$(this).find('a');
    $content.stop().animate({ 
        'marginLeft'      :'-20px',
        'rotate'    : '10deg'
    },400).find('img').stop().animate({
        'width'     : '150px'
    },400);;
}
				
//mouseleave event on each thumb
function downImage(){
    var $content    =$(this).find('a');
    var r           =Math.floor(Math.random()*41)-20;
    $content.stop().animate({
        'marginTop' : '10px'
    },400).find('img').stop().animate({
        'rotate'    : r + 'deg'
    },400);
}
function decreaseSizeImage(){
    var $content    =$(this).find('a');
    var r           =Math.floor(Math.random()*41)-20;
    $content.stop().animate({      
        'marginLeft':'0px',
        'rotate'    : '0deg'
    },400).find('img').stop().animate({
        'width'     : '110px'
    },400);;
}
				
//resize function based on windows size
function resize($image){
    var widthMargin		= 50
    var heightMargin 	= 200;
					
    var windowH      = $(window).height()-heightMargin;
    var windowW      = $(window).width()-widthMargin;
    var theImage     = new Image();
    theImage.src     = $image.attr("src");
    var imgwidth     = theImage.width;
    var imgheight    = theImage.height;

    if((imgwidth > windowW)||(imgheight > windowH)){
        if(imgwidth > imgheight){
            var newwidth = windowW;
            var ratio = imgwidth / windowW;
            var newheight = imgheight / ratio;
            theImage.height = newheight;
            theImage.width= newwidth;
            if(newheight>windowH){
                var newnewheight = windowH;
                var newratio = newheight/windowH;
                var newnewwidth =newwidth/newratio;
                theImage.width = newnewwidth;
                theImage.height= newnewheight;
            }
        }
        else{
            var newheight = windowH;
            var ratio = imgheight / windowH;
            var newwidth = imgwidth / ratio;
            theImage.height = newheight;
            theImage.width= newwidth;
            if(newwidth>windowW){
                var newnewwidth = windowW;
                var newratio = newwidth/windowW;
                var newnewheight =newheight/newratio;
                theImage.height = newnewheight;
                theImage.width= newnewwidth;
            }
        }
    }
    $image.css({
        'width':theImage.width+'px',
        'height':theImage.height+'px'
    });
}